/* eslint-disable */
/**
数据验证（表单验证）
作者 zjw
*/
module.exports = {
  error: '',
  check: function (data, rule) {
    for (let i = 0; i < rule.length; i++) {
      if (rule[i].isPassCheck) {
        continue;
      }
      if (!rule[i].checkType) {
        return true;
      }
      if (!rule[i].name) {
        return true;
      }
      if (!rule[i].errorMsg) {
        return true;
      }
      if (!data[rule[i].name] && data[rule[i].name] !== 0) {
        this.error = rule[i].errorMsg;
        return false;
      }
      switch (rule[i].checkType) {
        case 'string':
          var reg = new RegExp('^.{' + rule[i].checkRule + '}$');
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'zhDecimal':
          // /[\u4e00-\u9fa5\.]/
          var reg = /^[\u4E00-\u9FFF.]+$/;
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'int':
          var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$');
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'between':
          if (!this.isNumber(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          var minMax = rule[i].checkRule.split(',');
          minMax[0] = Number(minMax[0]);
          minMax[1] = Number(minMax[1]);
          if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'betweenD':
          var reg = /^-?[1-9][0-9]?$/;
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          var minMax = rule[i].checkRule.split(',');
          minMax[0] = Number(minMax[0]);
          minMax[1] = Number(minMax[1]);
          if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'betweenF':
          var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          var minMax = rule[i].checkRule.split(',');
          minMax[0] = Number(minMax[0]);
          minMax[1] = Number(minMax[1]);
          if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'same':
          if (data[rule[i].name] != rule[i].checkRule) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'notsame':
          if (data[rule[i].name] == rule[i].checkRule) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'email':
          var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'phoneno':
          var reg = /^[1,9][0-9]{10,10}$/;
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'phonenoOther':
          // var reg = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
          var reg = /\d+\-.*/;
          var regz = /[^0-9-]/g;
          var pattern = /^\d+$/g;
          if (regz.test(data[rule[i].name]) && !data[rule[i].name].match(pattern)) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'idcard':
          // 身份证号码格式校验
          var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
          if (reg.test(data[rule[i].name]) === false) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'zipcode':
          var reg = /^[0-9]{6}$/;
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'reg':
          var reg = new RegExp(rule[i].checkRule);
          if (!reg.test(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'in':
          if (rule[i].checkRule.indexOf(data[rule[i].name]) == -1) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
        case 'notnull':
          if ((data[rule[i].name] == null || data[rule[i].name].length < 1) && data[rule[i].name] != 0) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
      }
    }
    return true;
  },
  isNumber: function (checkVal) {
    let reg = /^-?[1-9][0-9]?.?[0-9]*$/;
    return reg.test(checkVal);
  }
};
